﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using __Able.Network.Utils;
using __Able.Core.Utilities;

namespace PDM.Packet.RequestPacket
{
    public class LoginRequestPacket : PDMPacket
    {
        /// <summary>
        /// 包版本
        /// </summary>
        private static readonly byte[] version = { 1, 0, 0, 0 };
        /// <summary>
        /// 指令
        /// </summary>
        private static readonly ushort token = 0x0001;
        /// <summary>
        /// 
        /// </summary>
        public string Username { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        public string Password { get; private set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public LoginRequestPacket()
            : base(version, token)
        {

        }

        public LoginRequestPacket(ByteBuffer buf)
            : base(version, token, buf)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public void Fill(string username, string password)
        {
            this.Username = username.Trim();
            this.Password = password.Trim();

            base.Fill();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="buf"></param>
        protected override void PutBody(ByteBuffer buf)
        {
            // byte[32]
            buf.Put(Util.GetBytes(Crypto.MD5Encrypt(this.Password)));
            // byte[]
            buf.Put(Util.GetBytes(this.Username));
        }

        protected override void ParseBody(ByteBuffer buf)
        {
            this.Password = Util.GetString(buf.GetByteArray(32));
            this.Username = Util.GetString(buf.GetByteArray(buf.Length - 7 - 32));
        }
    }
}
